
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

@SuppressWarnings("serial")
public class CommandLine extends JPanel implements KeyListener, ActionListener
{
	ChatPanel chatPanel;
	JTextField commandText;
	
	public CommandLine(ChatPanel area)
	{
		chatPanel = area;
		
		setLayout(new BorderLayout());
		setBorder(BorderFactory.createEmptyBorder(4, 5, 2, 5));
		
		JLabel CommandLineTitle = new JLabel("Message:");
		CommandLineTitle.setBorder(BorderFactory.createEmptyBorder(0, 3, 0, 2));
		add(CommandLineTitle,BorderLayout.WEST);
		
		commandText = new JTextField();
		commandText.addKeyListener(this);
		add(commandText,BorderLayout.CENTER);
		
		JButton CommandButton = new JButton("Send");
		CommandButton.addActionListener(this);
		add(CommandButton,BorderLayout.EAST);
	}

	@Override
	public void keyPressed(KeyEvent e) {
		
	}

	@Override
	public void keyReleased(KeyEvent e) 
	{
		if(e.getKeyCode() == KeyEvent.VK_ENTER)
			sendMessage();
	}

	@Override
	public void keyTyped(KeyEvent e) {
		
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0)
	{
		sendMessage();
	}
	
	public void sendMessage()
	{
		String text = getText().trim().replace(">", "&gt;").replace("<", "&lt;");
		if(!text.equals(""))
			chatPanel.sendMessage(text);
	}
	
	public synchronized String getText()
	{
		String text = commandText.getText();
		commandText.setText("");
		return text;
	}
}
